package com.margo.project.web.client.action;

import java.io.File;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.margo.project.web.client.model.MargoGirlVO;

import io.swagger.annotations.Api;

/**
 * 测试RestTemplate
 * @author fxb208
 *
 */
@Api(tags={"RestTemplat"})
@RestController
@RequestMapping("/rest")
public class MargoRestTemplateController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	Logger logger=LoggerFactory.getLogger(this.getClass());
	/**
	 * content-type=application/x-www-form-urlencoded
	 * @param name
	 * @param age
	 * @return
	 */
	@PostMapping("form")
	public String form() {
		String url="http://localhost:8080/rest/form";
		//请求头
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		//请求体
		MultiValueMap<String, Object> body=new LinkedMultiValueMap<String, Object>();
		body.add("name", "margo");
		body.add("age", 27);
		HttpEntity<MultiValueMap<String, Object>> httpEntity=
				new HttpEntity<MultiValueMap<String,Object>>(body, headers);
		String result=restTemplate.postForObject(url, httpEntity, String.class);
		return result;
	}
	
	/**
	 * 	这种不行
	 * @return
	 */
	@PostMapping("form2")
	public String form2() {
		String url="http://localhost:8080/rest/form";
		//请求头
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		//请求体
		HashMap<String, Object> body=new HashMap<String, Object>();
		body.put("name", "margo");
		body.put("age", 27);
		HttpEntity<HashMap<String, Object>> httpEntity=
				new HttpEntity<HashMap<String,Object>>(body, headers);
		String result=restTemplate.postForObject(url, httpEntity, String.class);
		return result;
	}
	
	/**
	 * content-type=application/json
	 * @param margoGirlVO
	 * @return
	 * @throws JsonProcessingException 
	 */
	@PostMapping("/json")
	public String json() throws JsonProcessingException {
		String url="http://localhost:8080/rest/json";
		
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		MargoGirlVO margoGirlVO=new MargoGirlVO();
		margoGirlVO.setName("margo");
		margoGirlVO.setAge(27);
		ObjectMapper objectMapper=new ObjectMapper();
		String param=objectMapper.writeValueAsString(margoGirlVO);
		
		HttpEntity<String> requestEntity =new HttpEntity<String>(param, headers);
		String result=restTemplate.postForObject(url, requestEntity, String.class);
		return result;
	}
	
	/**
	 * content-type=application/json
	 * @param margoGirlVO
	 * @return
	 * @throws JsonProcessingException 
	 */
	@PostMapping("/json2")
	public String json2() {
		String url="http://localhost:8080/rest/json";
		
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		MargoGirlVO margoGirlVO=new MargoGirlVO();
		margoGirlVO.setName("margo");
		margoGirlVO.setAge(27);
//		ObjectMapper objectMapper=new ObjectMapper();
//		String param=objectMapper.writeValueAsString(margoGirlVO);
		
		HttpEntity<MargoGirlVO> requestEntity =new HttpEntity<MargoGirlVO>(margoGirlVO, headers);
		String result=restTemplate.postForObject(url, requestEntity, String.class);
		return result;
	}
	
	/**
	 * content-type= multipart/form-data
	 * @param file
	 * @param name
	 * @return
	 */
	@PostMapping("/file")
	public String file() {
		//url
		String url="http://localhost:8080/rest/file";
		//请求头
		HttpHeaders headers=new HttpHeaders();
		//请求体
		MultiValueMap<String, Object> body=new LinkedMultiValueMap<String, Object>();
		File file=new File("F:\\mask\\7.jpg");
		Resource resource = new FileSystemResource(file);
		body.add("file", resource);
		body.add("name", "margo");
		HttpEntity<MultiValueMap<String, Object>> httpEntity=
				new HttpEntity<MultiValueMap<String,Object>>(body, headers);
		String result=restTemplate.postForObject(url, httpEntity, String.class);
		logger.info(result);
		return result;
	}
}
